import { Effect } from 'dva';
import { Reducer } from 'redux';
import { produce, Draft } from 'immer';

import {
  applyDeviceLists,
  applyDeviceTreeData,
  postAddDevice,
  getDictDataList,
  getDictTypeList,
  userList,
  deleteUser,
  queryRoleList,
  queryByRoleInfo,
  queryByRoleUser,
  insertUserRole,
  updateRole,
  insertRole,
  deleteRole,
  deleteRoleUser,
  unallocatedList,
  checkRoleName,
  listBySystemId,
  menuListBySystemId,
  postAddMenu,
  allocatedList,
  deptList,
  deleteDept,
  addDept,
} from '@/services/systemDevice';

// 设备权限管理 列表
export interface DeviceListsItem {
  createBy: string; //
  createTime: string; //创建时间
  params: object; //
  remark: string; //备注
  roleId: number; //角色Id
  roleName: string; // 角色名称
  searchValue: string; //
  status: number; // 状态（0正常，1禁用）
  updateBy: string; //
  updateTime: string; //
}
// 设备权限管理 树形结构
export interface DeviceTreeDataItem {
  deviceId: number; //设备ID ,
  deviceName: string; // 设备名称 ,
  deviceTypeId: string; //设备类型ID ,
  fatherId: number; // 设备类型父级ID ,
  roleDevicePowerId: number; // 角色ID ,
  stuts: string; // 勾选状态(1是，0否) ,
  typeName: string; // 设备类型名称
}
export interface StateType {
  applyDeviceLists: DeviceListsItem[]; // 设备权限管理
  applyDeviceTreeData: DeviceTreeDataItem[]; //设备权限管理树形数据
}

export interface SystemDeviceModelType {
  namespace: string;
  state: StateType;
  effects: {
    applyDeviceLists: Effect;
    applyDeviceTreeData: Effect;
    postAddDevice: Effect;
    dictDataList: Effect;
    dictTypeList: Effect;
    userList: Effect;
    deleteUser: Effect;
    queryRoleList: Effect;
    queryByRoleInfo: Effect;
    queryByRoleUser: Effect;
    insertUserRole: Effect;
    updateRole: Effect;
    insertRole: Effect;
    deleteRole: Effect;
    deleteRoleUser: Effect;
    unallocatedList: Effect;
    checkRoleName: Effect;
    listBySystemId: Effect;
    menuListBySystemId: Effect;
    postAddMenu: Effect;
    allocatedList: Effect;
    deptList: Effect;
    deleteDept: Effect;
    addDept: Effect;
    setActiveTab: Effect;
    setRolePageNum: Effect;
  };
  reducers: {
    change_applyDeviceLists: Reducer<StateType>;
    change_applyDeviceTreeData: Reducer<StateType>;
    change_postAddDevice: Reducer<StateType>;
    change_getDictDataList: Reducer<StateType>;
    change_getDictTypeList: Reducer<StateType>;
    change_userList: Reducer<StateType>;
    change_deleteUser: Reducer<StateType>;
    change_deptList: Reducer<StateType>;
    change_deleteDept: Reducer<StateType>;
    change_setActiveTab: Reducer<StateType>;
    change_setRolePageNum: Reducer<StateType>;
    change_postAddMenu: Reducer<StateType>;
  };
}
const Model: SystemDeviceModelType = {
  namespace: 'systemDevice',

  state: {
    applyDeviceLists: [],
    applyDeviceTreeData: [],
    postAddDevice: [],
    dictDataList: {},
    dictTypeList: {},
    userList: {},
    deleteUser: [],
    queryRoleList: [],
    queryByRoleInfo: {},
    queryByRoleUser: [],
    insertUserRole: {},
    updateRole: {},
    insertRole: {},
    deleteRole: {},
    deleteRoleUser: {},
    unallocatedList: [],
    checkRoleName: {},
    listBySystemId: {},
    menuListBySystemId: {},
    postAddMenu: {},
    allocatedList: [],
    deptList: {},
    deleteDept: [],
    addDept: [],
    activeTab: '',
    rolePageNum: '',
  },

  effects: {
    *addDept({ payload }, { call, put }) {
      const [err, data] = yield call(addDept, payload);
      if (!err) yield put({ type: 'change_addDept', payload: data });
    },
    *deleteDept({ payload }, { call, put }) {
      const [err, data] = yield call(deleteDept, payload);
      if (!err) yield put({ type: 'change_deleteDept', payload: data });
    },
    *deptList({ payload }, { call, put }) {
      const [err, data] = yield call(deptList, payload);
      console.log(data);
      if (!err) yield put({ type: 'change_deptList', payload: data });
    },
    *deleteUser({ payload }, { call, put }) {
      const [err, data] = yield call(deleteUser, payload);
      if (!err) yield put({ type: 'change_deleteUser', payload: data });
    },
    *userList({ payload }, { call, put }) {
      const [err, data] = yield call(userList, payload);
      if (!err) yield put({ type: 'change_userList', payload: data });
    },
    *applyDeviceLists({ payload }, { call, put }) {
      const [err, data] = yield call(applyDeviceLists, payload);
      if (!err) yield put({ type: 'change_applyDeviceLists', payload: data });
    },
    *applyDeviceTreeData({ payload }, { call, put }) {
      const [err, data] = yield call(applyDeviceTreeData, payload);
      if (!err) yield put({ type: 'change_applyDeviceTreeData', payload: data });
    },
    *postAddDevice({ payload }, { call, put }) {
      const [err, data] = yield call(postAddDevice, payload);
      if (!err) yield put({ type: 'change_postAddDevice', payload: data });
    },
    *getDictDataList({ payload }, { call, put }) {
      const [err, data] = yield call(getDictDataList, payload);
      if (!err) yield put({ type: 'change_getDictDataList', payload: data });
    },
    *getDictTypeList({ payload }, { call, put }) {
      const [err, data] = yield call(getDictTypeList, payload);
      console.log(err, data);
      if (!err) yield put({ type: 'change_getDictTypeList', payload: data });
    }, //以下是角色管理页面接口
    *queryRoleList({ payload }, { call, put }) {
      const [err, data] = yield call(queryRoleList, payload);
      if (!err) yield put({ type: 'change_queryRoleList', payload: data });
    },
    *queryByRoleInfo({ payload }, { call, put }) {
      const [err, data] = yield call(queryByRoleInfo, payload);
      if (!err) yield put({ type: 'change_queryByRoleInfo', payload: data });
    },
    *queryByRoleUser({ payload }, { call, put }) {
      const [err, data] = yield call(queryByRoleUser, payload);
      if (!err) yield put({ type: 'change_queryByRoleUser', payload: data });
    },
    *insertUserRole({ payload }, { call, put }) {
      const response = yield call(insertUserRole, payload);
      yield put({ type: 'change_insertUserRole', payload: response.data });
    },
    *updateRole({ payload }, { call, put }) {
      const response = yield call(updateRole, payload);
      yield put({ type: 'change_updateRole', payload: response.data });
    },
    *insertRole({ payload }, { call, put }) {
      const response = yield call(insertRole, payload);
      yield put({ type: 'change_insertRole', payload: response.data });
    },
    *deleteRole({ payload }, { call, put }) {
      const response = yield call(deleteRole, payload);
      yield put({ type: 'change_deleteRole', payload: response.data });
    },
    *deleteRoleUser({ payload }, { call, put }) {
      const response = yield call(deleteRoleUser, payload);
      yield put({ type: 'change_deleteRoleUser', payload: response.data });
    },
    *unallocatedList({ payload }, { call, put }) {
      const [err, data] = yield call(unallocatedList, payload);
      if (!err) yield put({ type: 'change_unallocatedList', payload: data });
    },
    *allocatedList({ payload }, { call, put }) {
      const [err, data] = yield call(allocatedList, payload);
      if (!err) yield put({ type: 'change_allocatedList', payload: data });
    },
    *checkRoleName({ payload }, { call, put }) {
      const response = yield call(checkRoleName, payload);
      yield put({ type: 'change_checkRoleName', payload: response.data });
    },
    *setActiveTab({ payload }, { put }) {
      yield put({ type: 'change_setActiveTab', payload });
      console.log(payload);
    },
    *setRolePageNum({ payload }, { put }) {
      yield put({ type: 'change_setRolePageNum', payload });
    },

    *listBySystemId({ payload }, { call, put }) {
      const [err, data] = yield call(listBySystemId, payload);
      if (!err) yield put({ type: 'change_listBySystemId', payload: data });
    },
    *menuListBySystemId({ payload }, { call, put }) {
      const [err, data] = yield call(menuListBySystemId, payload);
      if (!err) yield put({ type: 'change_menuListBySystemId', payload: data });
      localStorage.setItem('MenuPower', JSON.stringify(data));
    },
    *postAddMenu({ payload }, { call, put }) {
      const [err, data] = yield call(postAddMenu, payload);
      if (!err) yield put({ type: 'change_postAddMenu', payload: data });
    },
  },
  reducers: {
    change_deleteUser: produce((draft: Draft<StateType>, { payload }): void => {
      draft.userList = payload;
    }),
    change_userList: produce((draft: Draft<StateType>, { payload }): void => {
      draft.userList = payload;
    }),
    change_applyDeviceLists: produce((draft: Draft<StateType>, { payload }): void => {
      draft.applyDeviceLists = payload;
    }),
    change_applyDeviceTreeData: produce((draft: Draft<StateType>, { payload }): void => {
      draft.applyDeviceTreeData = payload;
    }),
    change_postAddDevice: produce((draft: Draft<StateType>, { payload }): void => {
      draft.postAddDevice = payload;
    }),
    change_getDictDataList: produce((draft: Draft<StateType>, { payload }): void => {
      draft.dictDataList = payload;
    }),
    change_getDictTypeList: produce((draft: Draft<StateType>, { payload }): void => {
      draft.dictTypeList = payload;
    }), //以下是角色管理页面接口
    change_queryRoleList: produce((draft: Draft<StateType>, { payload }): void => {
      draft.queryRoleList = payload;
    }),
    change_queryByRoleInfo: produce((draft: Draft<StateType>, { payload }): void => {
      draft.queryByRoleInfo = payload;
    }),
    change_queryByRoleUser: produce((draft: Draft<StateType>, { payload }): void => {
      draft.queryByRoleUser = payload;
    }),
    change_insertUserRole: produce((draft: Draft<StateType>, { payload }): void => {
      draft.insertUserRole = payload;
    }),
    change_updateRole: produce((draft: Draft<StateType>, { payload }): void => {
      draft.updateRole = payload;
    }),
    change_insertRole: produce((draft: Draft<StateType>, { payload }): void => {
      draft.insertRole = payload;
    }),
    change_deleteRole: produce((draft: Draft<StateType>, { payload }): void => {
      draft.deleteRole = payload;
    }),
    change_deleteRoleUser: produce((draft: Draft<StateType>, { payload }): void => {
      draft.deleteRoleUser = payload;
    }),
    change_unallocatedList: produce((draft: Draft<StateType>, { payload }): void => {
      draft.unallocatedList = payload;
    }),
    change_allocatedList: produce((draft: Draft<StateType>, { payload }): void => {
      draft.allocatedList = payload;
    }),
    change_checkRoleName: produce((draft: Draft<StateType>, { payload }): void => {
      draft.checkRoleName = payload;
    }),
    change_listBySystemId: produce((draft: Draft<StateType>, { payload }): void => {
      draft.listBySystemId = payload;
    }),
    change_menuListBySystemId: produce((draft: Draft<StateType>, { payload }): void => {
      draft.menuListBySystemId = payload;
    }),
    change_postAddMenu: produce((draft: Draft<StateType>, { payload }): void => {
      draft.postAddMenu = payload;
    }),
    change_addDept: produce((draft: Draft<StateType>, { payload }): void => {
      draft.addDept = payload;
    }),
    change_deleteDept: produce((draft: Draft<StateType>, { payload }): void => {
      draft.deleteDept = payload;
    }),
    change_deptList: produce((draft: Draft<StateType>, { payload }): void => {
      draft.deptList = payload;
    }),
    change_setActiveTab: produce((draft: Draft<StateType>, { payload }): void => {
      draft.activeTab = localStorage.getItem('systemMenu');
    }),
    change_setRolePageNum: produce((draft: Draft<StateType>, { payload }): void => {
      draft.rolePageNum = localStorage.getItem('rolePageNum');
    }),
  },
};
export default Model;
